package com.dofast.bt.mvp.view.activity.basket;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.dofast.bt.R;
import com.dofast.bt.base.BaseMvpActivity;
import com.dofast.bt.bean.ShoppingCartBean;
import com.dofast.bt.mvp.presenter.basket.BuyListPresenter;
import com.dofast.bt.util.PresenterFactory;
import com.dofast.bt.util.PresenterLoder;

import org.xutils.ex.DbException;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * username:Created by hsj on 2016/12/25 0025.
 * class：
 * 类描述：购买列表界面
 */

public class BuyListActivity extends BaseMvpActivity<BuyListPresenter, BuyListView> implements BuyListView, AdapterView.OnItemClickListener {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_edit)
    TextView tvEdit;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_selcect_all)
    ImageView ivSelcectAll;
    @BindView(R.id.tv_select_all)
    TextView tvSelectAll;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    @BindView(R.id.ll_select_all)
    LinearLayout llSelectAll;
    @BindView(R.id.lv_basket)
    ListView lvBasket;

    @Override
    protected int getResourcesId() {
        return R.layout.fragment_basket;
    }

    @Override
    protected void initView() {
        initTitle();
        presenter.initData();
    }

    @Override
    protected void addOnClickListener() {
        lvBasket.setOnItemClickListener(this);
    }

    public static void gotoBuyListActivity(Context context) {
        Intent intent = new Intent(context, BuyListActivity.class);
        context.startActivity(intent);

    }

    @Override
    public Loader<BuyListPresenter> onCreateLoader(int id, Bundle args) {
        return new PresenterLoder<>(this, new PresenterFactory<BuyListPresenter>() {
            @Override
            public BuyListPresenter create() {
                return new BuyListPresenter();
            }
        });
    }

    @Override
    public void initAdapter(Adapter adapter) {
        lvBasket.setAdapter((ListAdapter) adapter);
    }

    @Override
    public void showSelectAll() {
        ivSelcectAll.setImageResource(R.mipmap.select_normal);
    }

    @Override
    public void hideSelectAll() {
        ivSelcectAll.setImageResource(R.mipmap.select_default);
    }

    @Override
    public void gotoSubmitOrderActivity(List<ShoppingCartBean> list) {
        SubmitOderActivity.gotoSubmitOrderActivity(BuyListActivity.this, list);
    }


    @Override
    public void initTitle() {
        tvTitle.setText("购物车");
//        tvEdit.setVisibility(View.GONE);
        ivBack.setVisibility(View.VISIBLE);
    }

    @Override
    public void gotoActivity(Class<?> tClass, boolean isFinish) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);

    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        presenter.itemClick(position);
    }

    /**
     * 编辑
     */

    @Override
    public void setEdit(String edit) {
        tvEdit.setText(edit);
    }

    @Override
    public void setSubmit(String submit) {
        tvCommit.setText(submit);
    }



    @OnClick({R.id.tv_edit, R.id.iv_back, R.id.iv_selcect_all, R.id.tv_select_all, R.id.tv_commit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_edit:
                presenter.edit();
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_selcect_all:
                presenter.selectAll();
                break;
            case R.id.tv_select_all:
                presenter.selectAll();
                break;
            case R.id.tv_commit:
                presenter.submit();
                break;
        }
    }
}
